/*
 * desc: 描述
 * Author: jayafs
 * Email: 1169655050@qq.com
 * Date: 2020-11-24 23:26:54
 * LastEditors: jayafs
 * LastEditTime: 2020-12-01 23:34:23
 */

import { useEffect } from 'react';
import { isEmpty } from 'project-libs';

let observer: IntersectionObserver;
export default function useObserverHook(
  ele: string,
  callback: (arg: IntersectionObserverEntry[]) => void,
  watch?: any[],
) {
  useEffect(() => {
    const node = document.querySelector(ele);
    if (node) {
      observer = new IntersectionObserver(entries => {
        callback && callback(entries);
      });
      observer.observe(node);
    }

    return () => {
      if (observer && node) {
        // 解绑元素
        observer.unobserve(node);

        // 停止监听
        observer.disconnect();
      }
    };
  }, watch);
}
